package com.example.ccj.b.c.custom.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.media.MediaUtils;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片展示Adapter
 * 2024年4月24日21:08:57
 */
public class LittleImageAdapter extends RecyclerView.Adapter<LittleImageAdapter.ViewHolder>{
    List<LocalMedia> pictureList;
    /**
     *
     * @param pictureList  图片列表
     */
    public LittleImageAdapter(List<LocalMedia> pictureList){
        this.pictureList = pictureList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).
                inflate(R.layout.adapter_little_picture, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LocalMedia picture = pictureList.get(position);
        ImageUtils.loadImage(holder.itemView.getContext(),picture.getAvailablePath(),holder.mIvLoadImg);
        //图片大图展示
        bigImage(holder, position);
    }

    /**
     * 大图展示
     * @param holder
     * @param position
     */
    private void bigImage(@NonNull ViewHolder holder, int position){
        //图片大图展示
        holder.mIvLoadImg.setOnClickListener(v -> {
            MediaUtils mediaUtils = new MediaUtils();
            mediaUtils.openPreview(ActivityUtils.getTopActivity(), position,false,new ArrayList<>(pictureList));
        });

    }

    @Override
    public int getItemCount() {
        return pictureList == null ? 0 : pictureList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        ImageView mIvLoadImg;
        MImageView selectImg;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvLoadImg = itemView.findViewById(R.id.adapter_little_picture_img);
            selectImg = itemView.findViewById(R.id.control_show_little_image_title_select_img);

        }
    }

}
